<?php
class Mageex_RelatedProduct_Block_Catalog_Product_List_Related extends Mage_Catalog_Block_Product_List_Related
{
	protected $_itemCollection;
	
    protected function _prepareData()
    {
        $product = Mage::registry('product');
			/* @var $product Mage_Catalog_Model_Product */
		if( $product){
			$this->_itemCollection = $product->getRelatedProductCollection()
				->addAttributeToSelect('required_options')
				->addAttributeToSort('position', 'asc')
				->addStoreFilter()
			;
			Mage::getResourceSingleton('checkout/cart')->addExcludeProductFilter($this->_itemCollection,
				Mage::getSingleton('checkout/session')->getQuoteId()
			);
			$this->_addProductAttributesAndPrices($this->_itemCollection);

	//        Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($this->_itemCollection);
			Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($this->_itemCollection);

			$this->_itemCollection->load();

			foreach ($this->_itemCollection as $product) {
				$product->setDoNotUseCategoryId(true);
			}
		}
        return $this;
    }

    protected function _beforeToHtml()
    {
        $this->_prepareData();
        return parent::_beforeToHtml();
    }
	
	public function getFeatureProduct()
	{
		$_helper = Mage::helper('catalog/output');
		$storeId =  Mage::app()->getStore()->getId();
		$collection = Mage::getResourceModel('catalog/product_collection')
			->addAttributeToSelect('*')
			->addAttributeToSelect('price')
			->setStoreId($storeId)
			->addStoreFilter($storeId)
			->addAttributeToFilter('is_feature', 1);
		return $collection;
	}
	public function countFeatureProduct()
	{
		if(count($this->getFeatureProduct())>0){
			return true;
		}
		return false;
	}
	public function haveItems()
	{
		if(count($this->_itemCollection)>0){
			return true;
		}
		return false;
	}
    public function getItems()
    {
        return $this->_itemCollection;
    }
	public function isEnableRelatedProduct()
	{
		if(Mage::helper('relatedproduct/config')->isRelatedProductEnable()){
			return true;
		}
		return false;
	}
	public function isReplaceNative()
	{
		if(Mage::helper('relatedproduct/config')->isReplaceNative()){
			return true;
		}
		return false;
	}
	public function getRelatedProduct()
	{
		$arrNotIn = array();
		$product = Mage::registry('product');
		//echo $product->getId();
		if(!$this->isReplaceNative()){
			if(count($this->_itemCollection)>0){
				
				foreach($this->_itemCollection as $_item){
					$arrNotIn[] = $_item->getId();
				}
			}
		}
		$relatedProducts = Mage::helper('relatedproduct/data')->getRelatedProduct($product, $arrNotIn);
		return $relatedProducts;
	}
	public function countRelatedProduct()
	{
		if(count($this->getRelatedProduct())>0){
			return true;
		}
		return false;
	}
}